home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / ohlutil.zip / RMDIR.C < prev    next >
C/C++ Source or Header  |  1990-06-22  |  3KB  |  143 lines

  1. /* rmdir -- remove directories
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Usage: rmdir [-p] [+path] dir...
  19.  
  20.    Options:
  21.    -p, +path        Remove any parent dirs that are explicitly mentioned
  22.             in an argument, if they become empty after the
  23.             argument file is removed.
  24.  
  25.    David MacKenzie <djm@ai.mit.edu>  */
  26.  
  27. #include <stdio.h>
  28. #include <sys/types.h>
  29. #include "system.h"
  30. #include "getopt.h"
  31.  
  32. #ifdef STDC_HEADERS
  33. #include <errno.h>
  34. #include <stdlib.h>
  35. #else
  36. extern int errno;
  37. #endif
  38.  
  39. void remove_parents ();
  40. void error ();
  41. void strip_trailing_slashes ();
  42. void usage ();
  43.  
  44. /* If nonzero, remove empty parent directories. */
  45. int empty_paths;
  46.  
  47. /* The name this program was run with. */
  48. char *program_name;
  49.  
  50. struct option longopts[] =
  51. {
  52.   {"path", 0, &empty_paths, 1},
  53.   {NULL, 0, NULL, 0}
  54. };
  55.  
  56. void
  57. main (argc, argv)
  58.      int argc;
  59.      char **argv;
  60. {
  61.   int errors = 0;
  62.   int optc;
  63.   int ind;
  64.  
  65.   program_name = argv[0];
  66.   empty_paths = 0;
  67.  
  68.   while ((optc = getopt_long (argc, argv, "p", longopts, &ind)) != EOF)
  69.     {
  70.       switch (optc)
  71.     {
  72.     case 0:            /* Long option. */
  73.       break;
  74.     case 'p':
  75.       empty_paths = 1;
  76.       break;
  77.     default:
  78.       usage ();
  79.     }
  80.     }
  81.  
  82.   if (optind == argc)
  83.     usage ();
  84.   
  85.   for (; optind < argc; ++optind)
  86.     {
  87.       strip_trailing_slashes (argv[optind]);
  88.       if (rmdir (argv[optind]) != 0)
  89.     {
  90.       error (0, errno, "%s", argv[optind]);
  91.       errors = 1;
  92.     }
  93.       else if (empty_paths)
  94.     remove_parents (argv[optind]);
  95.     }
  96.  
  97.   exit (errors);
  98. }
  99.  
  100. /* Remove any empty parent directories of `path'.
  101.    Replaces '/' characters in `path' with NULs. */
  102.  
  103. void
  104. remove_parents (path)
  105.      char *path;
  106. {
  107.   char *slash;
  108.  
  109.   do
  110.     {
  111.       slash = rindex (path, '/');
  112.       if (slash == NULL)
  113.     break;
  114.       /* Remove any characters after the slash, skipping any extra
  115.      slashes in a row. */
  116.       while (slash > path && *slash == '/')
  117.     --slash;
  118.       slash[1] = 0;
  119.     }
  120.   while (rmdir (path) == 0);
  121. }
  122.  
  123. /* Remove trailing slashes from PATH; they cause some system calls to fail. */
  124.  
  125. void
  126. strip_trailing_slashes (path)
  127.      char *path;
  128. {
  129.   int last;
  130.  
  131.   last = strlen (path) - 1;
  132.   while (last > 0 && path[last] == '/')
  133.     path[last--] = '\0';
  134. }
  135.  
  136. void
  137. usage ()
  138. {
  139.   fprintf (stderr, "Usage: %s [-p] [+path] dir...\n",
  140.        program_name);
  141.   exit (1);
  142. }
  143.